package cn.virens.database.serviceimpl.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.virens.database.common.BaseServiceImpl;
import cn.virens.database.mysql.mapper.sys.SysResourceMapper;
import cn.virens.database.mysql.model.sys.SysResource;
import cn.virens.database.service.sys.SysResourceService;
import cn.virens.exception.APIException;

@Service
public class SysResourceServiceImpl extends BaseServiceImpl<SysResource, SysResourceMapper> implements SysResourceService {

	@Autowired
	public SysResourceServiceImpl(SysResourceMapper mBaseMapper) {
		super(mBaseMapper);
	}

	/**
	 * 先插入，然后更新资源代码
	 * @throws APIException 
	 */
	@Override
	public int insert(SysResource record) throws APIException {
		int i = super.insert(record);
		int u = super.update(record);

		return i | u;
	}

	@Override
	public int update(SysResource record) throws APIException {
		record.setCode(null);
		record.setCreateTime(null);

		return super.update(record);
	}

	@Override
	public int updateParent(String code, String parent) {
		return mBaseMapper.updateParent(code, parent);
	}

	@Override
	public List<SimpleResource> getSimpleResource(String parent) {
		List<Map<String, Object>> list = mBaseMapper.getSimpleResource(parent);

		SimpleResource result = new SimpleResource();

		for (Map<String, Object> map : list) {
			result.addChildren(new SimpleResource(map));
		}

		return result.getChildren();
	}

	@Override
	public List<SimpleResource> getMenusByAccount(String account) {
		List<Map<String, Object>> list = mBaseMapper.getResourcesByAccount(account);

		SimpleResource result = new SimpleResource();

		for (Map<String, Object> map : list) {
			result.addChildren(new MenuResource(map));
		}

		return result.getChildren();
	}

	@Override
	protected List<?> doPageQuery(Map<String, Object> param) throws APIException {
		return mBaseMapper.getAll(MapUtils.getString(param, "name"));
	}

	@Override
	protected Class<SysResource> getGenericSuperclass() {
		return SysResource.class;
	}

	/**
	 * 菜单资源构造类
	 * 
	 * @作者 : loioi
	 * @创建时间 :2017年8月3日 上午10:30:02
	 */
	public class MenuResource extends SimpleResource {
		private String icon;
		private String linkurl;
		private String identity;

		public MenuResource() {
			super();
		}

		public MenuResource(Map<String, Object> map) {
			super(map);
			this.icon = MapUtils.getString(map, "icon");
			this.linkurl = MapUtils.getString(map, "linkurl");
			this.identity = MapUtils.getString(map, "identity");
		}

		public String getIcon() {
			return icon;
		}

		public String getLinkurl() {
			return linkurl;
		}

		public String getIdentity() {
			return identity;
		}

		public void setIdentity(String identity) {
			this.identity = identity;
		}

	}

	/**
	 * 简单层级资源构造类
	 * 
	 * @作者 : loioi
	 * @创建时间 :2017年8月3日 下午12:42:43
	 */
	public class SimpleResource {
		private String code;
		private String name;
		private List<SimpleResource> children;

		public SimpleResource() {
			this.code = "";
			this.children = new ArrayList<>();
		}

		public SimpleResource(Map<String, Object> map) {
			this.code = MapUtils.getString(map, "code");
			this.name = MapUtils.getString(map, "name");
		}

		public boolean addChildren(SimpleResource res) {
			if (CollectionUtils.isNotEmpty(children)) {
				for (SimpleResource r1 : children) {
					if (r1.addChildren(res)) {
						return true;
					}
				}
			}

			if (StringUtils.startsWith(res.code, code)) {
				if (children == null) this.children = new ArrayList<>();

				return this.children.add(res);
			}

			return false;
		}

		public String getCode() {
			return code;
		}

		public String getName() {
			return name;
		}

		public List<SimpleResource> getChildren() {
			return children;
		}
	}
}
